package main.java.org.service.serviceImpl;

import main.java.Exam.pojo.Entity.TeacherStudent;
import main.java.Exam.pojo.Entity.User;
import main.java.Exam.pojo.tableVO.StudentScore;
import main.java.Exam.pojo.tableVO.StudentVo;
import main.java.org.mapper.StudentMapper;
import main.java.org.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceimpl implements StudentService {


    @Autowired
    private StudentMapper studentMapper;
    ////获取全部学生账号姓名
    @Override
    public List<StudentVo> getStudentData(int status) {
        List<User>list=studentMapper.getIdStudent(status);
        List<StudentVo>studentVos=new ArrayList<>();

        for (User user : list) {
            StudentVo studentVo=new StudentVo();
            studentVo.setUserAccount(user.getUserAccount());
            studentVo.setName(user.getUserName());
            studentVo.setGender(user.getGender());
            studentVos.add(studentVo);
        }
        return studentVos;
    }

    @Override
    public List<StudentScore> getStudentTotalScore(StudentVo studentVo) {
     /*   User user=studentMapper.getAccountUser(studentVo.getUserAccount());
        List<TeacherStudent>list=studentMapper.getStdentIdScore(user.getId());
        List<StudentScore>studentScores=new ArrayList<>();
        StudentScore studentScore=new StudentScore();
        studentScore.setStudentName(studentVo.getName());
        for (TeacherStudent teacherStudent : list) {
            studentScore.setTestName(teacherStudent.getTestTitle());
            studentScore.setScore(teacherStudent.getScore());
            studentScores.add(studentScore);

        }
        return studentScores;*/
        User user = studentMapper.getAccountUser(studentVo.getUserAccount());
        if (user == null) {
            throw new RuntimeException("学生不存在");
        }
        List<TeacherStudent> list = studentMapper.getStdentIdScore(user.getId());
        List<StudentScore> studentScores = new ArrayList<>();

        for (TeacherStudent teacherStudent : list) {
            StudentScore studentScore = new StudentScore();
            studentScore.setStudentName(studentVo.getName());
            studentScore.setTestName(teacherStudent.getTestTitle());
            studentScore.setScore(teacherStudent.getScore());
            Integer pastTime = studentMapper.getPastTimeByUserAndTest(user.getId(), teacherStudent.getTestId());
            studentScore.setPastTime(pastTime);
            studentScores.add(studentScore);
        }
        return studentScores;
    }
}
